/***************
 * Organization Users Panel
 */
AIR2.Organization.Users = function() {
    var p = new AIR2.UI.Panel({
        colspan: 2,
        title: 'Users',
        showTotal: true,
        iconCls: 'air2-icon-user',
        storeData: AIR2.Organization.USRDATA,
        url: AIR2.Organization.ORGURL + '/users',
        itemSelector: '.air2-org-usr',
        tools: ['->',{
            xtype: 'air2button',
            air2type: 'CLEAR',
            iconCls: 'air2-icon-add',
            tooltip: 'Create User',
            hidden: !AIR2.Organization.ORGDATA.authz.may_manage,
            handler: function() {
                AIR2.User.Create({
                    originEl: this.el,
                    orgUUID: AIR2.Organization.ORGUUID,
                    callback: function(success, msg) {
                        if (success) p.reload();
                    }
                });
            }
        }],
        tpl: new Ext.XTemplate(
            '<table><tpl for=".">' +
              '<tr class="air2-org-usr">' +
                '<td>{[AIR2.Format.userName(values.User, true)]}</td>' +
                '<td><tpl if="uo_home_flag"><span>{uo_user_title}</span></tpl></td>' +
                '<td><span class="role">{[values.AdminRole.ar_name]}</span></td>' +
                '<td style="width:44px">' +
                  '<tpl if="uo_home_flag"><span class="air2-icon-home" ext:qtip="Home Organization"></span></tpl>' +
                  '<tpl if="uo_notify_flag"><span class="air2-icon-email" ext:qtip="Notifications On"></span></tpl>' +
                '</td>' +
                '<td><p>Member since {[AIR2.Format.date(values.uo_cre_dtim)]}</p></td>' +
              '</tr>' +
            '</tpl></table>',
            {compiled: true, disableFormats: true}
        ),
        editModal: {
            title: 'Organization Users',
            cls: 'air2-org-usr',
            allowAdd: AIR2.Organization.ORGDATA.authz.may_manage ? 'Add User' : false,
            width: 500,
            items: {
                xtype: 'air2liveeditor',
                url: AIR2.Organization.ORGURL + '/users',
                sortField: 'User.user_last_name',
                sortDir: 'ASC',
                pageSize: 50,
                allowEdit: AIR2.Organization.ORGDATA.authz.may_manage,
                allowDelete: AIR2.Organization.ORGDATA.authz.may_manage,
                hideButtons: true,
                editForm: {
                    layout:'column',
                    items: [{
                        xtype: 'container',
                        columnWidth: .43,
                        layout: 'form',
                        labelWidth: 30,
                        items: {
                            xtype: 'air2searchbox',
                            name: 'user_uuid',
                            fieldLabel: 'User',
                            searchUrl: AIR2.HOMEURL + '/user',
                            baseParams: {excl_org: AIR2.Organization.ORGUUID},
                            valueField: 'user_uuid',
                            displayField: 'user_username',
                            msgTarget: 'under',
                            disabled: true,
                            formatComboListItem: function(v) {
                                return AIR2.Format.userName(v);
                            },
                            width: 140
                        }
                    },{
                        xtype: 'container',
                        columnWidth: .32,
                        layout: 'form',
                        labelWidth: 30,
                        items: {
                            xtype: 'air2combo',
                            name: 'uo_ar_id',
                            fieldLabel: 'Role',
                            store: new Ext.data.JsonStore({
                                data: AIR2.Organization.ROLELIST
                            }),
                            valueField: 'ar_id',
                            displayField: 'ar_name',
                            width: 90
                        }
                    },{
                        xtype: 'container',
                        columnWidth: .25,
                        layout: 'form',
                        labelWidth: 38,
                        items: {
                            xtype: 'checkbox',
                            name: 'uo_notify_flag',
                            fieldLabel: 'Notify'
                        }
                    }],
                    listeners: {
                        beforeedit: function(form, rec) {
                            var f = form.getForm();
                            f.setValues(rec.data);

                            // setup user field
                            var usrfld = form.find('name', 'user_uuid')[0];
                            usrfld.setDisabled(!rec.phantom);
                            usrfld.setRawValue(rec.data.User.user_username);

                            // setup role field
                            var rolefld = form.find('name', 'uo_ar_id')[0];
                            rolefld.setRawValue(rec.data.AdminRole.ar_name);
                        },
                        beforesave: function(form, rec) {
                            var f = form.getForm();
                            f.updateRecord(rec);
                        }
                    }
                },
                tpl: new Ext.XTemplate(
                    '<div class="user" style="width:100%">' +
                      '<span class="flags">' +
                        '<tpl if="uo_home_flag"><span class="icons air2-icon-home" ext:qtip="Home Organization"></span></tpl>' +
                        '<tpl if="uo_notify_flag"><span class="icons air2-icon-email" ext:qtip="Notifications On"></span></tpl>' +
                      '</span>' +
                      '<span class="username">' +
                        '{[AIR2.Format.userName(values.User, true)]}' +
                      '</span>' +
                      '<span class="role">' +
                        '{[values.AdminRole.ar_name]}' +
                      '</span>' +
                      '<button class="air2-rowedit"></button>' +
                      '<button class="air2-rowdelete"></button>' +
                    '</div>',
                    {compiled: true, disableFormats: true}
                )
            }
        }
    });
    return p;
}
